<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>WindowsJScript | lilei</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/images/icons/logo.png">
    <link rel="apple-touch-icon" href="/images/icons/logo.png">
    <link rel="manifest" href="/manifest.json">
    <script type="text/javascript" src="/assets/js/load.js"></script>
    <meta name="description" content="个人网站">
    <meta name="twitter:title" content="WindowsJScript">
    <meta name="twitter:description" content="">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:url" content="http://www.lileiblog.xyz/Shell/WindowsJScript.html">
    <meta property="og:type" content="article">
    <meta property="og:title" content="WindowsJScript">
    <meta property="og:description" content="">
    <meta property="og:url" content="http://www.lileiblog.xyz/Shell/WindowsJScript.html">
    <meta property="og:site_name" content="lilei">
    <meta itemprop="name" content="WindowsJScript">
    <meta itemprop="description" content="">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/images/icons/logo.png">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="keywords" content="lilei,vue,vuepress,vuepress-theme,theme,主题,vuepress主题,blog
        ,vuepress-blog,java,python,shell,sql,golang,script,shell script,nginx,windows,去广告,androd,google drive
        ,one drive,idea,eclipse,git,小程序">
    <meta name="referrer" content="never">
    
    <link rel="preload" href="/assets/css/0.styles.8932179b.css" as="style"><link rel="preload" href="/assets/js/app.269009d6.js" as="script"><link rel="preload" href="/assets/js/3.974b9720.js" as="script"><link rel="preload" href="/assets/js/62.cb825d1a.js" as="script"><link rel="prefetch" href="/assets/js/10.5915e99b.js"><link rel="prefetch" href="/assets/js/11.ff732762.js"><link rel="prefetch" href="/assets/js/12.8bb86d91.js"><link rel="prefetch" href="/assets/js/13.18bde5b0.js"><link rel="prefetch" href="/assets/js/14.314fae34.js"><link rel="prefetch" href="/assets/js/15.0a4ff48f.js"><link rel="prefetch" href="/assets/js/16.ffaf1503.js"><link rel="prefetch" href="/assets/js/17.49a31a6e.js"><link rel="prefetch" href="/assets/js/18.7e6443bf.js"><link rel="prefetch" href="/assets/js/19.6880cc9a.js"><link rel="prefetch" href="/assets/js/20.1fb3f255.js"><link rel="prefetch" href="/assets/js/21.0cd39eea.js"><link rel="prefetch" href="/assets/js/22.392d85cb.js"><link rel="prefetch" href="/assets/js/23.f50adb15.js"><link rel="prefetch" href="/assets/js/24.43e324e1.js"><link rel="prefetch" href="/assets/js/25.e3663205.js"><link rel="prefetch" href="/assets/js/26.da141fb8.js"><link rel="prefetch" href="/assets/js/27.6c05235a.js"><link rel="prefetch" href="/assets/js/28.c124245a.js"><link rel="prefetch" href="/assets/js/29.d185870d.js"><link rel="prefetch" href="/assets/js/30.8587d422.js"><link rel="prefetch" href="/assets/js/31.dc382b56.js"><link rel="prefetch" href="/assets/js/32.20ba32e9.js"><link rel="prefetch" href="/assets/js/33.6ecd7de9.js"><link rel="prefetch" href="/assets/js/34.2b91c13a.js"><link rel="prefetch" href="/assets/js/35.66ed0733.js"><link rel="prefetch" href="/assets/js/36.ce7bca84.js"><link rel="prefetch" href="/assets/js/37.d00bb5ac.js"><link rel="prefetch" href="/assets/js/38.c3ecbb6f.js"><link rel="prefetch" href="/assets/js/39.308f4175.js"><link rel="prefetch" href="/assets/js/4.520f6ecc.js"><link rel="prefetch" href="/assets/js/40.f4b98bc2.js"><link rel="prefetch" href="/assets/js/41.95fae64f.js"><link rel="prefetch" href="/assets/js/42.fe4f877b.js"><link rel="prefetch" href="/assets/js/43.ca8f222e.js"><link rel="prefetch" href="/assets/js/44.4d4ee79e.js"><link rel="prefetch" href="/assets/js/45.79178b33.js"><link rel="prefetch" href="/assets/js/46.c3817d0b.js"><link rel="prefetch" href="/assets/js/47.b8252443.js"><link rel="prefetch" href="/assets/js/48.df9d5d3b.js"><link rel="prefetch" href="/assets/js/49.d812fdb2.js"><link rel="prefetch" href="/assets/js/5.f1a24a16.js"><link rel="prefetch" href="/assets/js/50.55cfcaa8.js"><link rel="prefetch" href="/assets/js/51.b8608b02.js"><link rel="prefetch" href="/assets/js/52.52c5e00d.js"><link rel="prefetch" href="/assets/js/53.3a9bd5fb.js"><link rel="prefetch" href="/assets/js/54.a4240c13.js"><link rel="prefetch" href="/assets/js/55.01c4b876.js"><link rel="prefetch" href="/assets/js/56.f29549a3.js"><link rel="prefetch" href="/assets/js/57.01438a04.js"><link rel="prefetch" href="/assets/js/58.29bb4626.js"><link rel="prefetch" href="/assets/js/59.e8b89408.js"><link rel="prefetch" href="/assets/js/6.8c478b39.js"><link rel="prefetch" href="/assets/js/60.bedb5381.js"><link rel="prefetch" href="/assets/js/61.bdfbe584.js"><link rel="prefetch" href="/assets/js/63.a9516572.js"><link rel="prefetch" href="/assets/js/64.aa854ed7.js"><link rel="prefetch" href="/assets/js/65.285f883d.js"><link rel="prefetch" href="/assets/js/66.12849f37.js"><link rel="prefetch" href="/assets/js/67.c18b00e4.js"><link rel="prefetch" href="/assets/js/68.fa59daa9.js"><link rel="prefetch" href="/assets/js/69.e9d5dda4.js"><link rel="prefetch" href="/assets/js/7.cfb0d0ea.js"><link rel="prefetch" href="/assets/js/70.db126cce.js"><link rel="prefetch" href="/assets/js/71.6acb04cd.js"><link rel="prefetch" href="/assets/js/72.7ff0ab11.js"><link rel="prefetch" href="/assets/js/73.9a66da79.js"><link rel="prefetch" href="/assets/js/74.0a38a030.js"><link rel="prefetch" href="/assets/js/75.7b32dbb3.js"><link rel="prefetch" href="/assets/js/8.cb9c08e5.js"><link rel="prefetch" href="/assets/js/9.ad652cfe.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.110a1065.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8932179b.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/images/icons/logo.png" alt="lilei" class="logo"> <span class="site-name can-hide">lilei</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/Form/" class="nav-link">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link router-link-active">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/Form/" class="nav-link">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link router-link-active">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Shell</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Shell/" aria-current="page" class="sidebar-link">Shell</a></li><li><a href="/Shell/PowerShell.html" class="sidebar-link">PowerShell</a></li><li><a href="/Shell/ShellScript.html" class="sidebar-link">ShellScript</a></li><li><a href="/Shell/ShellWindows.html" class="sidebar-link">ShellWindows</a></li><li><a href="/Shell/Shell命令.html" class="sidebar-link">Shell命令</a></li><li><a href="/Shell/WindowsBatch.html" class="sidebar-link">WindowsBatch</a></li><li><a href="/Shell/WindowsJScript.html" aria-current="page" class="active sidebar-link">WindowsJScript</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Shell/WindowsJScript.html#flag" class="sidebar-link">Flag</a></li><li class="sidebar-sub-header"><a href="/Shell/WindowsJScript.html#bat和js混合编程" class="sidebar-link">BAT和JS混合编程</a></li><li class="sidebar-sub-header"><a href="/Shell/WindowsJScript.html#activexobject" class="sidebar-link">ActiveXObject</a></li><li class="sidebar-sub-header"><a href="/Shell/WindowsJScript.html#js函数封装" class="sidebar-link">js函数封装</a></li></ul></li><li><a href="/Shell/WindowsScript.html" class="sidebar-link">WindowsScript</a></li><li><a href="/Shell/WindowsVBScript.html" class="sidebar-link">WindowsVBScript</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="windowsjscript"><a href="#windowsjscript" class="header-anchor">#</a> WindowsJScript</h1> <p></p><div class="table-of-contents"><ul><li><a href="#flag">Flag</a></li><li><a href="#bat和js混合编程">BAT和JS混合编程</a><ul><li><a href="#参数传递">参数传递</a></li></ul></li><li><a href="#activexobject">ActiveXObject</a></li><li><a href="#js函数封装">js函数封装</a><ul><li><a href="#http请求">HTTP请求</a></li><li><a href="#解析xml">解析XML</a></li><li><a href="#解析html">解析HTML</a></li><li><a href="#图片格式转换">图片格式转换</a></li><li><a href="#设置壁纸">设置壁纸</a></li><li><a href="#刷新桌面">刷新桌面</a></li><li><a href="#获取系统信息">获取系统信息</a></li><li><a href="#解压zip">解压zip</a></li><li><a href="#下载7z">下载7z</a></li><li><a href="#数据库">数据库</a></li><li><a href="#开机启动">开机启动</a></li><li><a href="#获取所有com组件">获取所有COM组件</a></li><li><a href="#创建任务计划">创建任务计划</a></li></ul></li></ul></div><p></p> <h2 id="flag"><a href="#flag" class="header-anchor">#</a> Flag</h2> <ul><li><a href="https://www.wanweibaike.com/wiki-JScript" target="_blank" rel="noopener noreferrer">https://www.wanweibaike.com/wiki-JScript<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <blockquote><p><code>JScript</code>实现的<code>ECMAScript Edition 3</code>，也是<code>IE8</code>使用的引擎。然而，随着<code>V8</code>大放光彩，
微软放弃了之前规划的托管<code>JavaScript</code>计划（同期规划的<code>VB</code>变身为<code>VB.NET</code>活了下来），
<code>JScript</code>开发组另起炉灶搞了<code>Chakra</code>与<code>Node.js</code>一争长短，这也是<code>IE9</code>之后使用的<code>JS</code>引擎。</p></blockquote> <blockquote><p>在<code>JScript</code>中，永远不需要去实例化根对象<code>WScript</code>，正如同浏览器中的直接全局对象一样。</p></blockquote> <ul><li><a href="https://docs.microsoft.com/zh-cn/previous-versions//hbxc2t98(v=vs.85)" target="_blank" rel="noopener noreferrer">JScript (ECMAScript3)<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.php.cn/manual/view/14969.html" target="_blank" rel="noopener noreferrer">JScript参考手册<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.itminus.com/blog/categories/%E9%A3%8E%E8%AF%AD/ECMAScript/JScript" target="_blank" rel="noopener noreferrer">JScript<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="http://caibaojian.com/jscript" target="_blank" rel="noopener noreferrer">http://caibaojian.com/jscript<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <h2 id="bat和js混合编程"><a href="#bat和js混合编程" class="header-anchor">#</a> BAT和JS混合编程</h2> <ul><li><code>BAT</code>执行<code>JScript</code>原理 <a href="http://www.bathome.net/thread-33125-1-1.html" target="_blank" rel="noopener noreferrer">js/bat脚本混编新方案<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li>缺陷：修改了一个变量的值</li></ul> <div class="language-batch extra-class"><pre class="language-batch"><code><span class="token operator">@</span><span class="token command"><span class="token keyword">set</span> @a=<span class="token number">1</span>/*</span>
<span class="token operator">@</span><span class="token command"><span class="token keyword">echo</span> off</span>
<span class="token command"><span class="token keyword">start</span> wscript <span class="token parameter attr-name">-e</span><span class="token label property">:jscript</span> <span class="token string">&quot;%~f0&quot;</span></span>
<span class="token command"><span class="token keyword">pause</span></span>
<span class="token command"><span class="token keyword">goto</span> <span class="token label property">:EOF</span></span>
*/

// ****************************  JavaScript  *******************************

</code></pre></div><ul><li>缺陷：会清屏</li></ul> <div class="language-batch extra-class"><pre class="language-batch"><code><span class="token command"><span class="token keyword">echo</span>=<span class="token number">1</span>/*&gt;nul</span><span class="token operator">&amp;</span><span class="token operator">@</span><span class="token command"><span class="token keyword">cls</span></span>
<span class="token operator">@</span><span class="token command"><span class="token keyword">echo</span> off</span>
<span class="token command"><span class="token keyword">start</span> wscript <span class="token parameter attr-name">-e</span><span class="token label property">:jscript</span> <span class="token string">&quot;%~f0&quot;</span></span>
<span class="token command"><span class="token keyword">pause</span></span>
<span class="token command"><span class="token keyword">goto</span> <span class="token label property">:EOF</span></span>
*/

// ****************************  JavaScript  *******************************

</code></pre></div><ul><li>缺陷：暂时没有</li></ul> <div class="language-batch extra-class"><pre class="language-batch"><code><span class="token operator">@</span><span class="token command"><span class="token keyword">if</span> (<span class="token number">1</span>==<span class="token number">1</span></span><span class="token punctuation">)</span> <span class="token operator">@</span>end/*
<span class="token operator">@</span><span class="token command"><span class="token keyword">echo</span> off</span>
<span class="token command"><span class="token keyword">echo</span> Hello World!</span>
<span class="token command"><span class="token keyword">start</span> wscript <span class="token parameter attr-name">-e</span><span class="token label property">:jscript</span> <span class="token string">&quot;%~f0&quot;</span></span>
<span class="token command"><span class="token keyword">pause</span></span>
<span class="token command"><span class="token keyword">goto</span> <span class="token label property">:EOF</span></span>
*/

// ****************************  JavaScript  *******************************

</code></pre></div><ul><li>缺陷：暂时没有</li></ul> <div class="language-batch extra-class"><pre class="language-batch"><code><span class="token operator">@</span><span class="token command"><span class="token keyword">if</span> (<span class="token number">0</span></span><span class="token punctuation">)</span>==<span class="token punctuation">(</span><span class="token command"><span class="token keyword">0</span></span><span class="token punctuation">)</span> echo off
<span class="token command"><span class="token keyword">cscript</span> -nologo <span class="token parameter attr-name">-e</span><span class="token label property">:jscript</span> <span class="token variable">%~s0</span></span>
<span class="token command"><span class="token keyword">goto</span> <span class="token label property">:EOF</span></span>
<span class="token operator">@</span><span class="token command"><span class="token keyword">end</span></span>

// ****************************  JavaScript  *******************************

</code></pre></div><ul><li>缺陷：暂时没有</li></ul> <blockquote><p><code>1&gt;1</code> 的运行结果是个 <code>Boolean</code> 值，而 js 允许这种无意义的语句（其实对 eval 来说是有意义的）</p></blockquote> <div class="language-batch extra-class"><pre class="language-batch"><code><span class="token command"><span class="token keyword">1</span>&gt;<span class="token number">1</span>/* :</span>
<span class="token operator">@</span><span class="token command"><span class="token keyword">echo</span> off</span>
<span class="token command"><span class="token keyword">cscript</span> -nologo <span class="token parameter attr-name">-e</span><span class="token label property">:jscript</span> <span class="token variable">%0</span></span>
<span class="token command"><span class="token keyword">pause</span></span><span class="token operator">&amp;</span><span class="token command"><span class="token keyword">exit</span></span>
*/

// ****************************  JavaScript  *******************************

</code></pre></div><ul><li>缺陷：需要包裹html标签</li></ul> <div class="language-batch extra-class"><pre class="language-batch"><code>&lt;!-- :
<span class="token operator">@</span><span class="token command"><span class="token keyword">echo</span> off</span>
<span class="token comment">::for /f &quot;delims=&quot; %%a in ('mshta &quot;%~f0&quot;') do echo;%%a</span>
<span class="token command"><span class="token keyword">for</span> <span class="token parameter attr-name">/f</span> <span class="token string">&quot;delims=&quot;</span> <span class="token variable">%%a</span> <span class="token keyword">in</span> <span class="token punctuation">(</span><span class="token punctuation">'</span>mshta <span class="token string">&quot;%~f0&quot;</span><span class="token punctuation">'</span><span class="token punctuation">)</span> <span class="token keyword">do</span></span> <span class="token punctuation">(</span> <span class="token command"><span class="token keyword">set</span> <span class="token variable">filePath</span><span class="token operator">=</span><span class="token variable">%%a</span></span><span class="token punctuation">)</span>
<span class="token command"><span class="token keyword">echo</span> <span class="token variable">%filePath%</span></span>
<span class="token command"><span class="token keyword">pause</span></span><span class="token operator">&amp;</span><span class="token command"><span class="token keyword">exit</span> <span class="token parameter attr-name">/b</span></span>
--&gt;

&lt;input type=file id=f&gt;
&lt;script&gt;

<span class="token command"><span class="token keyword">f</span>.click(</span><span class="token punctuation">)</span>;

<span class="token command"><span class="token keyword">alert</span>(f.value</span><span class="token punctuation">)</span>;

<span class="token command"><span class="token keyword">var</span> sfo = new ActiveXObject('Scripting.FileSystemObject'</span><span class="token punctuation">)</span>;
// 获取TextStream对象.参数：0输入流,1输出流,2错误流.
<span class="token command"><span class="token keyword">sfo</span>.GetStandardStream(<span class="token number">1</span></span><span class="token punctuation">)</span>.Write<span class="token punctuation">(</span><span class="token command"><span class="token keyword">f</span>.value</span><span class="token punctuation">)</span>;

<span class="token command"><span class="token keyword">var</span> Shell = new ActiveXObject(<span class="token string">&quot;Shell.Application&quot;</span></span><span class="token punctuation">)</span>;
// 起始目录为：桌面
<span class="token command"><span class="token keyword">var</span> Folder = Shell.BrowseForFolder(<span class="token number">0</span>, <span class="token string">&quot;请选择文件夹&quot;</span>, <span class="token number">0</span></span><span class="token punctuation">)</span>;
<span class="token command"><span class="token keyword">if</span> (Folder != null</span><span class="token punctuation">)</span> {
    <span class="token command"><span class="token keyword">Folder</span> = Folder.items(</span><span class="token punctuation">)</span>;
    <span class="token command"><span class="token keyword">Folder</span> = Folder.item(</span><span class="token punctuation">)</span>;
    <span class="token command"><span class="token keyword">Folder</span> = Folder.Path;</span>
    <span class="token command"><span class="token keyword">sfo</span>.GetStandardStream(<span class="token number">1</span></span><span class="token punctuation">)</span>.Write<span class="token punctuation">(</span><span class="token command"><span class="token keyword">Folder</span></span><span class="token punctuation">)</span>;
}
<span class="token command"><span class="token keyword">close</span>(</span><span class="token punctuation">)</span>;

&lt;/script&gt;
</code></pre></div><h3 id="参数传递"><a href="#参数传递" class="header-anchor">#</a> 参数传递</h3> <blockquote><p>执行当前脚本中的JavaScript脚本：<code>cscript -nologo -e:jscript &quot;%~f0&quot;</code>，<code>%~f0</code>表示当前批处理的绝对路径,去掉引号的完整路径</p></blockquote> <div class="language-batch extra-class"><pre class="language-batch"><code><span class="token command"><span class="token keyword">1</span>&gt;<span class="token number">1</span>/* ::</span>
<span class="token comment">::  by bajins https://www.bajins.com</span>

<span class="token operator">@</span><span class="token command"><span class="token keyword">echo</span> off</span>

<span class="token comment">:: 开启延迟环境变量扩展</span>
<span class="token comment">:: 解决for或if中操作变量时提示ECHO OFF问题，用!!取变量</span>
<span class="token comment">:: 解决调用jscript提示命令错误问题</span>
<span class="token command"><span class="token keyword">setlocal</span> enabledelayedexpansion</span>

<span class="token comment">:: %~f0 表示当前批处理的绝对路径,去掉引号的完整路径，后面的是参数（组成方式：/key:value）</span>
<span class="token command"><span class="token keyword">cscript</span> -nologo <span class="token parameter attr-name">-e</span><span class="token label property">:jscript</span> <span class="token string">&quot;%~f0&quot;</span> <span class="token parameter attr-name">/func<span class="token punctuation">:</span></span>download <span class="token parameter attr-name">/url<span class="token punctuation">:</span></span><span class="token variable">%~1</span> <span class="token parameter attr-name">/path<span class="token punctuation">:</span></span><span class="token variable">%~2</span></span>

<span class="token comment">:: 无键，直接传入值</span>
<span class="token command"><span class="token keyword">cscript</span> -nologo <span class="token parameter attr-name">-e</span><span class="token label property">:jscript</span> <span class="token string">&quot;%~f0&quot;</span> download <span class="token variable">%~1</span> <span class="token variable">%~2</span> <span class="token variable">%~3</span> <span class="token variable">%~4</span> <span class="token variable">%~5</span> <span class="token variable">%~6</span> <span class="token variable">%~7</span> <span class="token variable">%~8</span> <span class="token variable">%~9</span></span>
<span class="token command"><span class="token keyword">goto</span> <span class="token label property">:EXIT</span></span>

<span class="token label property">:EXIT</span>
<span class="token comment">:: 结束延迟环境变量扩展和命令执行</span>
<span class="token command"><span class="token keyword">endlocal</span></span><span class="token operator">&amp;</span><span class="token command"><span class="token keyword">exit</span> <span class="token parameter attr-name">/b</span> <span class="token variable">%errorlevel%</span></span>
*/

// ****************************  JavaScript  *******************************

<span class="token command"><span class="token keyword">var</span> Argv = WScript.Arguments;</span>
<span class="token command"><span class="token keyword">for</span> (i = <span class="token number">0</span>; i &lt; Argv.Length; i++</span><span class="token punctuation">)</span> {
    <span class="token command"><span class="token keyword">WScript</span>.Echo(<span class="token string">&quot;参数：&quot;</span> + Argv(i</span><span class="token punctuation">)</span><span class="token punctuation">)</span>;
}

// 传参时指定键值，组成方式：/key:value
<span class="token command"><span class="token keyword">var</span> ArgvName = Argv.Named;</span>
<span class="token command"><span class="token keyword">var</span> func = ArgvName.Item(<span class="token string">&quot;func&quot;</span></span><span class="token punctuation">)</span>;
<span class="token command"><span class="token keyword">var</span> path = ArgvName.Item(<span class="token string">&quot;path&quot;</span></span><span class="token punctuation">)</span>;

// 无键，直接传入值
<span class="token command"><span class="token keyword">var</span> func = Argv(<span class="token number">0</span></span><span class="token punctuation">)</span>;
<span class="token command"><span class="token keyword">var</span> url = Argv(<span class="token number">1</span></span><span class="token punctuation">)</span>;
<span class="token command"><span class="token keyword">var</span> path = Argv(<span class="token number">2</span></span><span class="token punctuation">)</span>;

</code></pre></div><h2 id="activexobject"><a href="#activexobject" class="header-anchor">#</a> ActiveXObject</h2> <ul><li><a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Microsoft_Extensions/ActiveXObject" target="_blank" rel="noopener noreferrer">ActiveXObject<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://blog.csdn.net/chen_zw/article/details/9336375" target="_blank" rel="noopener noreferrer">ActiveXObject对象使用整理<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li><code>JScript</code>中<code>ActiveXObject</code>对象是启用并返回<code>Automation</code>对象的引用。</li> <li>使用方法：<code>var newObj = new ActiveXObject( servername.typename[, location])</code> <ul><li>其中<code>newObj</code>是必选项。要赋值为<code>ActiveXObject</code>的变量名。</li> <li><code>servername</code>是必选项。提供该对象的应用程序的名称。</li> <li><code>typename</code>是必选项。要创建的对象的类型或类。</li> <li><code>location</code>是可选项。创建该对象的网络服务器的名称。</li></ul></li></ul> <h2 id="js函数封装"><a href="#js函数封装" class="header-anchor">#</a> js函数封装</h2> <h3 id="http请求"><a href="#http请求" class="header-anchor">#</a> HTTP请求</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * HTTP请求
 * 查看方法属性：New-Object -ComObject &quot;WinHttp.WinHttpRequest.5.1&quot; | Get-Member
 *
 * @param method        GET,POST
 * @param url           请求地址
 * @param dataType      &quot;&quot;,text,stream,xml,json
 * @param data          数据，{key:value}格式
 * @param contentType   发送的数据类型：multipart/form-data、
 * application/x-www-form-urlencoded（默认）、text/plain
 * @returns {string|Document|any}
 */</span>
<span class="token keyword">function</span> <span class="token function">request</span><span class="token punctuation">(</span><span class="token parameter">method<span class="token punctuation">,</span> url<span class="token punctuation">,</span> dataType<span class="token punctuation">,</span> data<span class="token punctuation">,</span> contentType</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>url <span class="token operator">==</span> <span class="token string">&quot;&quot;</span> <span class="token operator">||</span> url <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> url<span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;请求url不能为空！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>method <span class="token operator">==</span> <span class="token string">&quot;&quot;</span> <span class="token operator">||</span> method <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> method<span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        method <span class="token operator">=</span> <span class="token string">&quot;GET&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token comment">// 把字符串转换为大写</span>
        method <span class="token operator">=</span> method<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>contentType <span class="token operator">==</span> <span class="token string">&quot;&quot;</span> <span class="token operator">||</span> contentType <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> contentType<span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        contentType <span class="token operator">=</span> <span class="token string">&quot;application/x-www-form-unlenconded&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> XMLHTTPVersions <span class="token operator">=</span> <span class="token punctuation">[</span>
        <span class="token string">'WinHttp.WinHttpRequest.5.1'</span><span class="token punctuation">,</span>
        <span class="token string">'WinHttp.WinHttpRequest.5.0'</span><span class="token punctuation">,</span>
        <span class="token string">'Msxml2.ServerXMLHTTP.6.0'</span><span class="token punctuation">,</span>
        <span class="token string">'Msxml2.ServerXMLHTTP.5.0'</span><span class="token punctuation">,</span>
        <span class="token string">'Msxml2.ServerXMLHTTP.4.0'</span><span class="token punctuation">,</span>
        <span class="token string">'Msxml2.ServerXMLHTTP.3.0'</span><span class="token punctuation">,</span>
        <span class="token string">'Msxml2.ServerXMLHTTP'</span><span class="token punctuation">,</span>
        <span class="token string">'MSXML2.XMLHTTP.6.0'</span><span class="token punctuation">,</span>
        <span class="token string">'MSXML2.XMLHTTP.5.0'</span><span class="token punctuation">,</span>
        <span class="token string">'MSXML2.XMLHTTP.4.0'</span><span class="token punctuation">,</span>
        <span class="token string">'MSXML2.XMLHTTP.3.0'</span><span class="token punctuation">,</span>
        <span class="token string">'MSXML2.XMLHTTP'</span><span class="token punctuation">,</span>
        <span class="token string">'Microsoft.XMLHTTP'</span>
    <span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> <span class="token constant">XMLHTTP</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> XMLHTTPVersions<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token constant">XMLHTTP</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span>XMLHTTPVersions<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            WScript<span class="token punctuation">.</span><span class="token function">Echo</span><span class="token punctuation">(</span>XMLHTTPVersions<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">&quot;:&quot;</span><span class="token punctuation">,</span> e<span class="token punctuation">.</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token constant">XMLHTTP</span><span class="token punctuation">.</span><span class="token function">SetTimeouts</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1800000</span><span class="token punctuation">,</span> <span class="token number">1800000</span><span class="token punctuation">,</span> <span class="token number">1800000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//将对象转化成为querystring形式</span>
    <span class="token keyword">var</span> paramarray <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>key <span class="token keyword">in</span> data<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        paramarray<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>key <span class="token operator">+</span> <span class="token string">&quot;=&quot;</span> <span class="token operator">+</span> data<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> params <span class="token operator">=</span> paramarray<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">&quot;&amp;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">switch</span> <span class="token punctuation">(</span>method<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token string">&quot;POST&quot;</span><span class="token operator">:</span>
            <span class="token comment">// 0异步、1同步</span>
            <span class="token constant">XMLHTTP</span><span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span>method<span class="token punctuation">,</span> url<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token constant">XMLHTTP</span><span class="token punctuation">.</span><span class="token function">SetRequestHeader</span><span class="token punctuation">(</span><span class="token string">&quot;CONTENT-TYPE&quot;</span><span class="token punctuation">,</span> contentType<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token constant">XMLHTTP</span><span class="token punctuation">.</span><span class="token function">Send</span><span class="token punctuation">(</span>params<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">default</span><span class="token operator">:</span>
            <span class="token comment">// 默认GET请求</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>params <span class="token operator">==</span> <span class="token string">&quot;&quot;</span> <span class="token operator">||</span> params<span class="token punctuation">.</span>length <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> params <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">// 0异步、1同步</span>
                <span class="token constant">XMLHTTP</span><span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span>method<span class="token punctuation">,</span> url<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                <span class="token constant">XMLHTTP</span><span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span>method<span class="token punctuation">,</span> url <span class="token operator">+</span> <span class="token string">&quot;?&quot;</span> <span class="token operator">+</span> params<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token constant">XMLHTTP</span><span class="token punctuation">.</span><span class="token function">SetRequestHeader</span><span class="token punctuation">(</span><span class="token string">&quot;CONTENT-TYPE&quot;</span><span class="token punctuation">,</span> contentType<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token constant">XMLHTTP</span><span class="token punctuation">.</span><span class="token function">Send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 把字符串转换为小写</span>
    dataType <span class="token operator">=</span> dataType<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">switch</span> <span class="token punctuation">(</span>dataType<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token string">&quot;text&quot;</span><span class="token operator">:</span>
            <span class="token keyword">return</span> <span class="token constant">XMLHTTP</span><span class="token punctuation">.</span>responseText<span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&quot;stream&quot;</span><span class="token operator">:</span>
            <span class="token keyword">return</span> <span class="token constant">XMLHTTP</span><span class="token punctuation">.</span>responseStream<span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&quot;json&quot;</span><span class="token operator">:</span>
            <span class="token keyword">return</span> <span class="token function">eval</span><span class="token punctuation">(</span><span class="token string">&quot;(&quot;</span> <span class="token operator">+</span> <span class="token constant">XMLHTTP</span><span class="token punctuation">.</span>responseText <span class="token operator">+</span> <span class="token string">&quot;)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">default</span><span class="token operator">:</span>
            <span class="token keyword">return</span> <span class="token constant">XMLHTTP</span><span class="token punctuation">.</span>responseBody<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>


<span class="token comment">/**
 * 下载文件
 * 查看方法属性：New-Object -ComObject &quot;ADODB.Stream&quot; | Get-Member
 *
 * @param url
 * @param directory 文件存储目录
 * @param filename  文件名，为空默认截取url中的文件名
 * @returns {string}
 */</span>
<span class="token keyword">function</span> <span class="token function">download</span><span class="token punctuation">(</span><span class="token parameter">url<span class="token punctuation">,</span> directory<span class="token punctuation">,</span> filename</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>url <span class="token operator">==</span> <span class="token string">&quot;&quot;</span> <span class="token operator">||</span> url <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> url<span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;请求url不能为空！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>directory <span class="token operator">==</span> <span class="token string">&quot;&quot;</span> <span class="token operator">||</span> directory <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> directory<span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;文件存储目录不能为空！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> fso <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Scripting.FileSystemObject&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 如果目录不存在</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>fso<span class="token punctuation">.</span><span class="token function">FolderExists</span><span class="token punctuation">(</span>directory<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 创建目录</span>
        <span class="token keyword">var</span> strFolderName <span class="token operator">=</span> fso<span class="token punctuation">.</span><span class="token function">CreateFolder</span><span class="token punctuation">(</span>directory<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>filename <span class="token operator">==</span> <span class="token string">&quot;&quot;</span> <span class="token operator">||</span> filename <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> filename<span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        filename <span class="token operator">=</span> url<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span>url<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 去掉文件名的特殊符号（包括之前的）字符</span>
        filename <span class="token operator">=</span> filename<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^.*(\&amp;|\=|\?|\/)</span><span class="token regex-delimiter">/</span><span class="token regex-flags">ig</span></span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> path <span class="token operator">=</span> directory <span class="token operator">+</span> <span class="token string">&quot;\\&quot;</span> <span class="token operator">+</span> filename<span class="token punctuation">;</span>
    <span class="token keyword">var</span> <span class="token constant">ADO</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;ADODB.Stream&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token constant">ADO</span><span class="token punctuation">.</span>Mode <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
    <span class="token constant">ADO</span><span class="token punctuation">.</span>Type <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token constant">ADO</span><span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token constant">ADO</span><span class="token punctuation">.</span><span class="token function">Write</span><span class="token punctuation">(</span><span class="token function">request</span><span class="token punctuation">(</span><span class="token string">&quot;GET&quot;</span><span class="token punctuation">,</span> url<span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token constant">ADO</span><span class="token punctuation">.</span><span class="token function">SaveToFile</span><span class="token punctuation">(</span>path<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token constant">ADO</span><span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 如果文件不存在</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>fso<span class="token punctuation">.</span><span class="token function">FileExists</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;文件下载失败&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> path<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="解析xml"><a href="#解析xml" class="header-anchor">#</a> 解析XML</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * 解析XML
 * 查看方法属性：New-Object -ComObject &quot;Msxml2.DOMDocument.6.0&quot; | Get-Member
 *
 * @param xml xml字符串或者文件路径
 * @returns {*}
 * @constructor
 */</span>
<span class="token keyword">function</span> <span class="token function">XMLParsing</span><span class="token punctuation">(</span><span class="token parameter">xml</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>xml <span class="token operator">==</span> <span class="token string">&quot;&quot;</span> <span class="token operator">||</span> xml <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> xml<span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;xml字符串或者文件路径不能为空！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> xmlDomVersions <span class="token operator">=</span> <span class="token punctuation">[</span>
        <span class="token string">'Msxml2.DOMDocument.6.0'</span><span class="token punctuation">,</span>
        <span class="token string">'Msxml2.DOMDocument.5.0'</span><span class="token punctuation">,</span>
        <span class="token string">'Msxml2.DOMDocument.4.0'</span><span class="token punctuation">,</span>
        <span class="token string">'MSXML2.DOMDocument.3.0'</span><span class="token punctuation">,</span>
        <span class="token string">'MSXML2.DOMDocument'</span><span class="token punctuation">,</span>
        <span class="token string">'Microsoft.XMLDOM'</span>
    <span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> xmlParser<span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> xmlDomVersions<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            xmlParser <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span>xmlDomVersions<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            WScript<span class="token punctuation">.</span><span class="token function">Echo</span><span class="token punctuation">(</span>XMLHTTPVersions<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">&quot;:&quot;</span><span class="token punctuation">,</span>  e<span class="token punctuation">.</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    xmlParser<span class="token punctuation">.</span>async <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> fso <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Scripting.FileSystemObject&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>fso<span class="token punctuation">.</span><span class="token function">FileExists</span><span class="token punctuation">(</span>xml<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 载入xml字符串</span>
        xmlParser<span class="token punctuation">.</span><span class="token function">loadXML</span><span class="token punctuation">(</span>xml<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token comment">// 载入xml文件</span>
        xmlParser<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>xml<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> xmlParser<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="解析html"><a href="#解析html" class="header-anchor">#</a> 解析HTML</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * 解析HTML
 * 查看方法属性：New-Object -ComObject &quot;htmlfile&quot; | Get-Member
 *
 * @param html html字符串或者文件路径
 * @returns {any}
 * @constructor
 */</span>
<span class="token keyword">function</span> <span class="token function">HtmlParsing</span><span class="token punctuation">(</span><span class="token parameter">html</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>html <span class="token operator">==</span> <span class="token string">&quot;&quot;</span> <span class="token operator">||</span> html <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> html<span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;html字符串或者文件路径不能为空！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> fso <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Scripting.FileSystemObject&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>fso<span class="token punctuation">.</span><span class="token function">FileExists</span><span class="token punctuation">(</span>html<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> htmlFile <span class="token operator">=</span> fso<span class="token punctuation">.</span><span class="token function">OpenTextFile</span><span class="token punctuation">(</span>html<span class="token punctuation">,</span> ForReading<span class="token punctuation">)</span><span class="token punctuation">;</span>
        html <span class="token operator">=</span> htmlFile<span class="token punctuation">.</span>ReadAll<span class="token punctuation">;</span>
        htmlFile<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// mhtmlfile</span>
    <span class="token keyword">var</span> htmlParser <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;htmlfile&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    htmlParser<span class="token punctuation">.</span>designMode <span class="token operator">=</span> <span class="token string">&quot;on&quot;</span><span class="token punctuation">;</span>
    htmlParser<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>html<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> htmlParser<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="图片格式转换"><a href="#图片格式转换" class="header-anchor">#</a> 图片格式转换</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * 图片格式转换
 *
 * @param imagePath 原始图片全路径
 * @param format    要转换的格式，后缀名
 * @returns {string}
 */</span>
<span class="token keyword">function</span> <span class="token function">imageTransform</span><span class="token punctuation">(</span><span class="token parameter">imagePath<span class="token punctuation">,</span> format</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> fso <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Scripting.FileSystemObject&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 如果文件不存在,就说明没有转换成功</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>fso<span class="token punctuation">.</span><span class="token function">FileExists</span><span class="token punctuation">(</span>imagePath<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;图片不存在或路径错误！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 转换后格式文件全路径</span>
    <span class="token keyword">var</span> formatPath <span class="token operator">=</span> imagePath<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(.+)\.[^\.]+$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">,</span> <span class="token string">&quot;$1&quot;</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;.&quot;</span> <span class="token operator">+</span> format<span class="token punctuation">;</span>
    <span class="token comment">// 如果转换后文件已存在</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>fso<span class="token punctuation">.</span><span class="token function">FileExists</span><span class="token punctuation">(</span>formatPath<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;要转换的格式文件已经存在！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 转小写</span>
    format <span class="token operator">=</span> format<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> wiaFormat <span class="token operator">=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">switch</span> <span class="token punctuation">(</span>format<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token string">&quot;bmp&quot;</span><span class="token operator">:</span>
            wiaFormat <span class="token operator">=</span> <span class="token string">&quot;{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&quot;png&quot;</span><span class="token operator">:</span>
            wiaFormat <span class="token operator">=</span> <span class="token string">&quot;{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&quot;gif&quot;</span><span class="token operator">:</span>
            wiaFormat <span class="token operator">=</span> <span class="token string">&quot;{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&quot;tiff&quot;</span><span class="token operator">:</span>
            wiaFormat <span class="token operator">=</span> <span class="token string">&quot;{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">default</span><span class="token operator">:</span>
            <span class="token comment">// 默认JPEG</span>
            wiaFormat <span class="token operator">=</span> <span class="token string">&quot;{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> img <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WIA.ImageFile&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    img<span class="token punctuation">.</span><span class="token function">LoadFile</span><span class="token punctuation">(</span>imagePath<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> imgps <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WIA.ImageProcess&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    imgps<span class="token punctuation">.</span>Filters<span class="token punctuation">.</span><span class="token function">Add</span><span class="token punctuation">(</span>imgps<span class="token punctuation">.</span><span class="token function">FilterInfos</span><span class="token punctuation">(</span><span class="token string">&quot;Convert&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>FilterID<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 转换格式</span>
    imgps<span class="token punctuation">.</span><span class="token function">Filters</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Properties</span><span class="token punctuation">(</span><span class="token string">&quot;FormatID&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Value <span class="token operator">=</span> wiaFormat<span class="token punctuation">;</span>
    <span class="token comment">// 图片质量</span>
    <span class="token comment">//imgps.Filters(1).Properties(&quot;Quality&quot;).Value = 5</span>
    imgps<span class="token punctuation">.</span><span class="token function">Apply</span><span class="token punctuation">(</span>img<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">SaveFile</span><span class="token punctuation">(</span>formatPath<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 如果文件不存在,就说明没有转换成功</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>fso<span class="token punctuation">.</span><span class="token function">FileExists</span><span class="token punctuation">(</span>formatPath<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;图片格式转为&quot;</span> <span class="token operator">+</span> format <span class="token operator">+</span> <span class="token string">&quot;失败&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> formatPath<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="设置壁纸"><a href="#设置壁纸" class="header-anchor">#</a> 设置壁纸</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * 设置桌面壁纸
 *
 * @param imagesPath 图片全路径
 */</span>
<span class="token keyword">function</span> <span class="token function">setWallpaper</span><span class="token punctuation">(</span><span class="token parameter">imagesPath</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> fso <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Scripting.FileSystemObject&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 如果文件不存在,就说明没有转换成功</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>fso<span class="token punctuation">.</span><span class="token function">FileExists</span><span class="token punctuation">(</span>imagePath<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;图片不存在或路径错误！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> shell <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WScript.shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// HKEY_CURRENT_USER</span>
    shell<span class="token punctuation">.</span><span class="token function">RegWrite</span><span class="token punctuation">(</span><span class="token string">&quot;HKCU\\Control Panel\\Desktop\\TileWallpaper&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;0&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 设置壁纸全路径</span>
    shell<span class="token punctuation">.</span><span class="token function">RegWrite</span><span class="token punctuation">(</span><span class="token string">&quot;HKCU\\Control Panel\\Desktop\\Wallpaper&quot;</span><span class="token punctuation">,</span> imagesPath<span class="token punctuation">)</span><span class="token punctuation">;</span>
    shell<span class="token punctuation">.</span><span class="token function">RegWrite</span><span class="token punctuation">(</span><span class="token string">&quot;HKCU\\Control Panel\\Desktop\\WallpaperStyle&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;2&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;REG_DWORD&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> shadowReg <span class="token operator">=</span> <span class="token string">&quot;HKCU\\Software\\Microsoft\\Windows\\CurrentVersion&quot;</span><span class="token punctuation">;</span>
    shell<span class="token punctuation">.</span><span class="token function">RegWrite</span><span class="token punctuation">(</span>shadowReg <span class="token operator">+</span> <span class="token string">&quot;\\Explorer\\Advanced\\ListviewShadow&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;REG_DWORD&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 如果桌面图标未透明，需要刷新组策略</span>
    <span class="token comment">//shell.Run(&quot;gpupdate /force&quot;, 0, true);</span>
    <span class="token comment">// 上面已经通过注册表设置了壁纸的参数，调用Windows api SystemParametersInfo刷新配置</span>
    <span class="token keyword">var</span> spi <span class="token operator">=</span> <span class="token string">&quot;RunDll32 USER32,SystemParametersInfo SPI_SETDESKWALLPAPER 0 \&quot;&quot;</span><span class="token punctuation">;</span>
    shell<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span>spi <span class="token operator">+</span> imagesPath <span class="token operator">+</span> <span class="token string">&quot;\&quot; SPIF_SENDWININICHANGE+SPIF_UPDATEINIFILE&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// for (var i = 0; i &lt; 30; i++) {</span>
    <span class="token comment">//     // 实时刷新桌面</span>
    <span class="token comment">//     shell.Run(&quot;RunDll32 USER32,UpdatePerUserSystemParameters&quot;, 0, true);</span>
    <span class="token comment">// }</span>
    shell<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token string">&quot;regsvr32.exe /s /n /i:/UserInstall %SystemRoot%\\system32\\themeui.dll&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><ul><li>这种方式更稳定</li></ul> <blockquote><p>使用API触发图片文件右键菜单上的 <code>设置为桌面背景(B)</code></p></blockquote> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * 设置桌面壁纸
 *
 * @param imagesPath 图片全路径
 */</span>
<span class="token keyword">function</span> <span class="token function">setWallpaper</span><span class="token punctuation">(</span><span class="token parameter">imagesPath</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> shApp <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Shell.Application&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取文件</span>
    <span class="token keyword">var</span> picFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Scripting.FileSystemObject&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">GetFile</span><span class="token punctuation">(</span>imagesPath<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取文件上的所有右键菜单项</span>
    <span class="token comment">//var items = shApp.NameSpace(picFile.ParentFolder.Path).ParseName(picFile.Name).Verbs();</span>
    <span class="token keyword">var</span> items <span class="token operator">=</span> shApp<span class="token punctuation">.</span><span class="token function">NameSpace</span><span class="token punctuation">(</span>picFile<span class="token punctuation">.</span>ParentFolder<span class="token punctuation">.</span>Path<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Items</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Item</span><span class="token punctuation">(</span>picFile<span class="token punctuation">.</span>Name<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Verbs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 遍历所有菜单项</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> items<span class="token punctuation">.</span>Count<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> item <span class="token operator">=</span> items<span class="token punctuation">.</span><span class="token function">Item</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 注意执行的脚本文件需要为简体中文编码</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">.</span>Name <span class="token operator">==</span> <span class="token string">&quot;设置为桌面背景(&amp;B)&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            item<span class="token punctuation">.</span><span class="token function">DoIt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="刷新桌面"><a href="#刷新桌面" class="header-anchor">#</a> 刷新桌面</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 切换到桌面</span>
<span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Shell.Application&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">ToggleDesktop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 刷新桌面</span>
<span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">SendKeys</span><span class="token punctuation">(</span><span class="token string">&quot;{F5}&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> WSHShell <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 切换到桌面</span>
<span class="token comment">//WSHShell.AppActivate(&quot;Program Manager&quot;)</span>
WSHShell<span class="token punctuation">.</span><span class="token function">AppActivate</span><span class="token punctuation">(</span>WSHShell<span class="token punctuation">.</span><span class="token function">SpecialFolders</span><span class="token punctuation">(</span><span class="token string">&quot;Desktop&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 刷新桌面</span>
WSHShell<span class="token punctuation">.</span><span class="token function">SendKeys</span><span class="token punctuation">(</span><span class="token string">&quot;{F5}&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 刷新桌面、任务栏、OSD（相当于重启资源管理器）</span>
<span class="token keyword">var</span> shell<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
shell<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token string">&quot;regsvr32.exe /s /n /i:/UserInstall %SystemRoot%\\system32\\themeui.dll&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 效果不太好，有时刷新成功，有时失败</span>
<span class="token keyword">var</span> shell<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
shell<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token string">&quot;RunDll32 USER32,UpdatePerUserSystemParameters&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// assoc文件关联时会自动刷新桌面，可能报错</span>
<span class="token keyword">var</span> shell<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
shell<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token string">&quot;assoc .=.&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>


<span class="token comment">// 重启资源管理器并恢复打开的目录，暂时不能使用</span>
<span class="token keyword">function</span> <span class="token function">restartExplorer</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> arrURL <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> shApp <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Shell.Application&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 遍历所有打开的窗口</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> shApp<span class="token punctuation">.</span><span class="token function">Windows</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Count<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> oWin <span class="token operator">=</span> shApp<span class="token punctuation">.</span><span class="token function">Windows</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Item</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 如果打开的窗口为资源管理器</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>oWin <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> oWin<span class="token punctuation">.</span>FullName<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">&quot;explorer.exe&quot;</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>oWin<span class="token punctuation">.</span>LocationURL <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                arrURL<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>oWin<span class="token punctuation">.</span>LocationURL<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                arrURL<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token comment">//oWin.Document.folder.title;</span>
            <span class="token comment">// 关闭当前打开的文件夹</span>
            <span class="token comment">//oWin.quit();</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 结束资源管理器进程</span>
    <span class="token keyword">var</span> shell <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WScript.Shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    shell<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token string">&quot;taskkill /f /im explorer.exe &gt;nul 2&gt;nul&amp;start explorer.exe&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 遍历并打开之前的窗口</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arrURL<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        shApp<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span>arrURL<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        shApp<span class="token punctuation">.</span><span class="token function">Explore</span><span class="token punctuation">(</span>arrURL<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="获取系统信息"><a href="#获取系统信息" class="header-anchor">#</a> 获取系统信息</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * 获取系统信息
 * 
 * @returns {{cpu_digits: *, cpu_core_number: *, system: string, os: *}}
 */</span>
<span class="token keyword">function</span> <span class="token function">getSystem</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> shell <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WScript.shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> system <span class="token operator">=</span> shell<span class="token punctuation">.</span><span class="token function">Environment</span><span class="token punctuation">(</span><span class="token string">&quot;SYSTEM&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 操作系统</span>
    <span class="token keyword">var</span> os <span class="token operator">=</span> <span class="token function">system</span><span class="token punctuation">(</span><span class="token string">&quot;OS&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">&quot;_&quot;</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token comment">// CPU位数</span>
    <span class="token keyword">var</span> cpuDigits <span class="token operator">=</span> <span class="token function">system</span><span class="token punctuation">(</span><span class="token string">&quot;PROCESSOR_ARCHITECTURE&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// CPU核心数</span>
    <span class="token keyword">var</span> cpuCoreNumber <span class="token operator">=</span> <span class="token function">system</span><span class="token punctuation">(</span><span class="token string">&quot;NUMBER_OF_PROCESSORS&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
        <span class="token string">&quot;os&quot;</span><span class="token operator">:</span> os<span class="token punctuation">,</span>
        <span class="token string">&quot;cpu_digits&quot;</span><span class="token operator">:</span> cpuDigits<span class="token punctuation">,</span>
        <span class="token string">&quot;cpu_core_number&quot;</span><span class="token operator">:</span> cpuCoreNumber<span class="token punctuation">,</span>
        <span class="token string">&quot;system&quot;</span><span class="token operator">:</span> os <span class="token operator">+</span> <span class="token string">&quot;_&quot;</span> <span class="token operator">+</span> cpuDigits
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * 获取当前系统位数
 * 
 * @returns {string}
 */</span>
<span class="token keyword">function</span> <span class="token function">systemDigits</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> locator <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WbemScripting.SWbemLocator&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 连接本地电脑</span>
    <span class="token keyword">var</span> service <span class="token operator">=</span> locator<span class="token punctuation">.</span><span class="token function">ConnectServer</span><span class="token punctuation">(</span><span class="token string">&quot;.&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取系统版本</span>
    <span class="token keyword">var</span> csResult <span class="token operator">=</span> service<span class="token punctuation">.</span><span class="token function">ExecQuery</span><span class="token punctuation">(</span><span class="token string">&quot;Select * from Win32_ComputerSystem&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 创建一个可枚举的对象</span>
    <span class="token keyword">var</span> cs <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Enumerator</span><span class="token punctuation">(</span>csResult<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">item</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> digits <span class="token operator">=</span> cs<span class="token punctuation">.</span>SystemType<span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>digits<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">&quot;86&quot;</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;i386&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>digits<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">&quot;64&quot;</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;amd64&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    WScript<span class="token punctuation">.</span><span class="token function">Echo</span><span class="token punctuation">(</span><span class="token string">&quot;不知道32位还是64位的&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    WScript<span class="token punctuation">.</span><span class="token function">Quit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * 获取当前系统版本
 * 
 * @returns {string}
 */</span>
<span class="token keyword">function</span> <span class="token function">osVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> locator <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WbemScripting.SWbemLocator&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 连接本地电脑</span>
    <span class="token keyword">var</span> service <span class="token operator">=</span> locator<span class="token punctuation">.</span><span class="token function">ConnectServer</span><span class="token punctuation">(</span><span class="token string">&quot;.&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取系统版本</span>
    <span class="token keyword">var</span> osResult <span class="token operator">=</span> service<span class="token punctuation">.</span><span class="token function">ExecQuery</span><span class="token punctuation">(</span><span class="token string">&quot;Select * from Win32_OperatingSystem&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 创建一个可枚举的对象</span>
    <span class="token keyword">var</span> os <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Enumerator</span><span class="token punctuation">(</span>osResult<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">item</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> caption <span class="token operator">=</span> os<span class="token punctuation">.</span>Caption<span class="token punctuation">;</span>
    <span class="token comment">// 截取version最后一个&quot;.&quot;的左面部分</span>
    <span class="token keyword">var</span> version <span class="token operator">=</span> os<span class="token punctuation">.</span>Version<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> version<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token string">&quot;.&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">switch</span> <span class="token punctuation">(</span>version<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">case</span> <span class="token string">&quot;5.2&quot;</span><span class="token operator">:</span>
            <span class="token keyword">return</span> <span class="token string">&quot;Windows Server 2003&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&quot;5.0&quot;</span><span class="token operator">:</span>
            <span class="token keyword">return</span> <span class="token string">&quot;Windows 2000&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&quot;5.1&quot;</span><span class="token operator">:</span>
            <span class="token keyword">return</span> <span class="token string">&quot;Windows XP&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&quot;6.0&quot;</span><span class="token operator">:</span>
            <span class="token keyword">return</span> <span class="token string">&quot;windows visita&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&quot;6.1&quot;</span><span class="token operator">:</span>
            <span class="token keyword">return</span> <span class="token string">&quot;windows 7&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> <span class="token string">&quot;10.0&quot;</span><span class="token operator">:</span>
            <span class="token keyword">return</span> <span class="token string">&quot;windows 10&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        defult<span class="token operator">:</span>
            <span class="token keyword">return</span> version<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="解压zip"><a href="#解压zip" class="header-anchor">#</a> 解压zip</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * 解压zip
 * 查看方法属性：New-Object -ComObject &quot;Shell.Application&quot; | Get-Member
 * 
 * @param zipFile       zip文件全路径
 * @param unDirectory   解压目录
 */</span>
<span class="token keyword">function</span> <span class="token function">unZip</span><span class="token punctuation">(</span><span class="token parameter">zipFile<span class="token punctuation">,</span> unDirectory</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> fso <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Scripting.FileSystemObject&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>fso<span class="token punctuation">.</span><span class="token function">FileExists</span><span class="token punctuation">(</span>zipFile<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;压缩文件不存在！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>fso<span class="token punctuation">.</span><span class="token function">GetExtensionName</span><span class="token punctuation">(</span>zipFile<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token string">&quot;zip&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;压缩文件后缀不为zip！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 如果解压目录不存在</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>fso<span class="token punctuation">.</span><span class="token function">FolderExists</span><span class="token punctuation">(</span>unDirectory<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">// 创建目录</span>
        fso<span class="token punctuation">.</span><span class="token function">CreateFolder</span><span class="token punctuation">(</span>unDirectory<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> objShell <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Shell.Application&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> objSource <span class="token operator">=</span> objShell<span class="token punctuation">.</span><span class="token function">NameSpace</span><span class="token punctuation">(</span>zipFile<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>objSource <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">&quot;无法解压文件！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    objShell<span class="token punctuation">.</span><span class="token function">NameSpace</span><span class="token punctuation">(</span>unDirectory<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">CopyHere</span><span class="token punctuation">(</span>objSource<span class="token punctuation">.</span><span class="token function">Items</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="下载7z"><a href="#下载7z" class="header-anchor">#</a> 下载7z</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * 下载7-Zip
 *
 * @param mode 下载模式：默认0不覆盖下载，1覆盖下载
 */</span>
<span class="token keyword">function</span> <span class="token function">download7z</span><span class="token punctuation">(</span><span class="token parameter">mode</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> shell <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WScript.shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 执行7z命令判断是否存在</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>shell<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token string">&quot;cmd /c 7za&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span><span class="token operator">!</span>mode <span class="token operator">||</span> mode <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> fso <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Scripting.FileSystemObject&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> storage <span class="token operator">=</span> <span class="token string">&quot;c:\\windows&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> exe <span class="token operator">=</span> storage <span class="token operator">+</span> <span class="token string">&quot;\\7z.exe&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> dll <span class="token operator">=</span> storage <span class="token operator">+</span> <span class="token string">&quot;\\7z.dll&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> filename <span class="token operator">=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">&quot;7z.*\-x64.msi&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;igm&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> url <span class="token operator">=</span> <span class="token string">&quot;https://sourceforge.net/projects/sevenzip/rss?path=/7-Zip&quot;</span><span class="token punctuation">;</span>
        filename <span class="token operator">=</span> reg<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token function">request</span><span class="token punctuation">(</span><span class="token string">&quot;get&quot;</span><span class="token punctuation">,</span> url<span class="token punctuation">,</span> <span class="token string">&quot;text&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        WScript<span class="token punctuation">.</span><span class="token function">Echo</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> url <span class="token operator">=</span> <span class="token string">&quot;https://www.7-zip.org/download.html&quot;</span><span class="token punctuation">;</span>
        filename <span class="token operator">=</span> reg<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span><span class="token function">request</span><span class="token punctuation">(</span><span class="token string">&quot;get&quot;</span><span class="token punctuation">,</span> url<span class="token punctuation">,</span> <span class="token string">&quot;text&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 当前文件所在目录</span>
    <span class="token keyword">var</span> dir <span class="token operator">=</span> fso<span class="token punctuation">.</span><span class="token function">GetFile</span><span class="token punctuation">(</span>WScript<span class="token punctuation">.</span>ScriptFullName<span class="token punctuation">)</span><span class="token punctuation">.</span>ParentFolder<span class="token punctuation">;</span>
    <span class="token keyword">var</span> msi <span class="token operator">=</span> dir <span class="token operator">+</span> <span class="token string">'\\'</span> <span class="token operator">+</span> filename<span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>fso<span class="token punctuation">.</span><span class="token function">FileExists</span><span class="token punctuation">(</span>msi<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        fso<span class="token punctuation">.</span><span class="token function">DeleteFile</span><span class="token punctuation">(</span>msi<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> zipDir <span class="token operator">=</span> dir <span class="token operator">+</span> <span class="token string">'\\7zip'</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>fso<span class="token punctuation">.</span><span class="token function">FolderExists</span><span class="token punctuation">(</span>zipDir<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        fso<span class="token punctuation">.</span><span class="token function">DeleteFolder</span><span class="token punctuation">(</span>zipDir<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">download</span><span class="token punctuation">(</span><span class="token string">&quot;https://www.7-zip.org/a/&quot;</span> <span class="token operator">+</span> filename<span class="token punctuation">,</span> dir<span class="token punctuation">,</span> filename<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 解压msi文件</span>
    shell<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token string">'msiexec /a &quot;'</span> <span class="token operator">+</span> msi <span class="token operator">+</span> <span class="token string">'&quot; /qn TARGETDIR=&quot;'</span> <span class="token operator">+</span> zipDir <span class="token operator">+</span> <span class="token string">'&quot;'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    fso<span class="token punctuation">.</span><span class="token function">CopyFile</span><span class="token punctuation">(</span>dir <span class="token operator">+</span> <span class="token string">&quot;\\7zip\\Files\\7-Zip\\7z.exe&quot;</span><span class="token punctuation">,</span> exe<span class="token punctuation">)</span><span class="token punctuation">;</span>
    fso<span class="token punctuation">.</span><span class="token function">CopyFile</span><span class="token punctuation">(</span>dir <span class="token operator">+</span> <span class="token string">&quot;\\7zip\\Files\\7-Zip\\7z.dll&quot;</span><span class="token punctuation">,</span> dll<span class="token punctuation">)</span><span class="token punctuation">;</span>
    fso<span class="token punctuation">.</span><span class="token function">DeleteFolder</span><span class="token punctuation">(</span>dir <span class="token operator">+</span> <span class="token string">&quot;\\7zip&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    fso<span class="token punctuation">.</span><span class="token function">DeleteFile</span><span class="token punctuation">(</span>msi<span class="token punctuation">)</span><span class="token punctuation">;</span>
    filename <span class="token operator">=</span> filename<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">&quot;x64.msi&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;extra.7z&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> exetra <span class="token operator">=</span> dir <span class="token operator">+</span> <span class="token string">'\\'</span> <span class="token operator">+</span> filename<span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>fso<span class="token punctuation">.</span><span class="token function">FileExists</span><span class="token punctuation">(</span>exetra<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        fso<span class="token punctuation">.</span><span class="token function">DeleteFile</span><span class="token punctuation">(</span>exetra<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">download</span><span class="token punctuation">(</span><span class="token string">&quot;https://www.7-zip.org/a/&quot;</span> <span class="token operator">+</span> filename<span class="token punctuation">,</span> dir<span class="token punctuation">,</span> filename<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// -aoa解压并覆盖文件，-o参数必须与值之间不能有空格</span>
    shell<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token string">'7z x -aoa &quot;'</span> <span class="token operator">+</span> exetra <span class="token operator">+</span> <span class="token string">'&quot; -o&quot;'</span> <span class="token operator">+</span> storage <span class="token operator">+</span> <span class="token string">'&quot; 7za.exe 7za.dll'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    fso<span class="token punctuation">.</span><span class="token function">DeleteFile</span><span class="token punctuation">(</span>exetra<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="数据库"><a href="#数据库" class="header-anchor">#</a> 数据库</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * 数据库
 * 查看方法属性：New-Object -ComObject &quot;ADODB.Connection&quot; | Get-Member
 */</span>
<span class="token keyword">function</span> <span class="token function">db</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">// 创建数据库对象   </span>
    <span class="token keyword">var</span> objdbConn <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;ADODB.Connection&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> strdsn <span class="token operator">=</span> <span class="token string">&quot;Driver={SQL Server}; Server=(local); Database=Test;UID=sa;PWD=123456&quot;</span><span class="token punctuation">;</span>
    <span class="token comment">// 打开数据源   </span>
    objdbConn<span class="token punctuation">.</span><span class="token function">Open</span><span class="token punctuation">(</span>strdsn<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 执行SQL的数据库查询   </span>
    <span class="token keyword">var</span> objrs <span class="token operator">=</span> objdbConn<span class="token punctuation">.</span><span class="token function">Execute</span><span class="token punctuation">(</span><span class="token string">&quot;Select * from test&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取字段数目   </span>
    <span class="token keyword">var</span> fdCount <span class="token operator">=</span> objrs<span class="token punctuation">.</span>Fields<span class="token punctuation">.</span>Count <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token comment">// 显示数据库内容   </span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span>objrs<span class="token punctuation">.</span><span class="token constant">EOF</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 显示每笔记录的字段</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> fdCount<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            WScript<span class="token punctuation">.</span><span class="token function">Echo</span><span class="token punctuation">(</span>objrs<span class="token punctuation">.</span><span class="token function">Fields</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">.</span>Value<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// 移到下一笔记录</span>
        objrs<span class="token punctuation">.</span><span class="token function">moveNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 关闭记录集合</span>
    objrs<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 关闭数据库链接</span>
    objdbConn<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="开机启动"><a href="#开机启动" class="header-anchor">#</a> 开机启动</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * 开机启动
 *
 * @param mode 为startup时是在开机启动目录中创建vbs脚本，否则添加开机启动注册表
 * @param arguments 向执行的程序或脚本传递相关联的参数
 */</span>
<span class="token keyword">function</span> <span class="token function">autoStart</span><span class="token punctuation">(</span><span class="token parameter">mode<span class="token punctuation">,</span> arguments</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>arguments <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> arguments <span class="token operator">!=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        arguments <span class="token operator">=</span> <span class="token string">&quot; &quot;</span> <span class="token operator">+</span> arguments<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> fileName <span class="token operator">=</span> WScript<span class="token punctuation">.</span>ScriptName<span class="token punctuation">;</span>
    fileName <span class="token operator">=</span> fileName<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> fileName<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token string">'.'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//fileName = fileName.substring(0, fileName.length-4);</span>
    <span class="token keyword">var</span> vbsFileName <span class="token operator">=</span> WScript<span class="token punctuation">.</span>ScriptFullName<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">&quot;.bat&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;.vbs&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">&quot;startup&quot;</span> <span class="token operator">==</span> mode<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 开机启动目录</span>
        <span class="token keyword">var</span> runDir <span class="token operator">=</span> <span class="token string">&quot;C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\StartUp\\&quot;</span><span class="token punctuation">;</span>
        vbsFileName <span class="token operator">=</span> runDir <span class="token operator">+</span> fileName <span class="token operator">+</span> <span class="token string">&quot;.vbs&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> fso <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Scripting.FileSystemObject&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 创建文件</span>
    <span class="token keyword">var</span> vbsFile <span class="token operator">=</span> fso<span class="token punctuation">.</span><span class="token function">CreateTextFile</span><span class="token punctuation">(</span>vbsFileName<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 填写数据，并增加换行符</span>
    vbsFile<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token string">&quot;Set shell = WScript.CreateObject(\&quot;WScript.Shell\&quot;)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    vbsFile<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token string">'shell.Run &quot;cmd /c '</span> <span class="token operator">+</span> WScript<span class="token punctuation">.</span>ScriptFullName <span class="token operator">+</span> arguments <span class="token operator">+</span> <span class="token string">'&quot;, 0, false'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 关闭文件</span>
    vbsFile<span class="token punctuation">.</span><span class="token function">Close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">&quot;startup&quot;</span> <span class="token operator">!=</span> mode<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 添加开机启动注册表</span>
        <span class="token keyword">var</span> shell <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WScript.shell&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">var</span> runRegBase <span class="token operator">=</span> <span class="token string">&quot;HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\&quot;</span><span class="token punctuation">;</span>
        shell<span class="token punctuation">.</span><span class="token function">RegWrite</span><span class="token punctuation">(</span>runRegBase <span class="token operator">+</span> fileName<span class="token punctuation">,</span> vbsFileName <span class="token operator">+</span> arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> vbsFileName<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="获取所有com组件"><a href="#获取所有com组件" class="header-anchor">#</a> 获取所有COM组件</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 列举本机所有的 com 组件 by http://www.bathome.net/thread-32948-1-1.html</span>
<span class="token keyword">function</span> <span class="token function">listcom</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> dict <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">'Scripting.Dictionary'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> oLoc <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;WbemScripting.SWbemLocator&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">var</span> oReg <span class="token operator">=</span> oLoc<span class="token punctuation">.</span><span class="token function">ConnectServer</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token string">&quot;root\\default&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Get</span><span class="token punctuation">(</span><span class="token string">&quot;StdRegProv&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> oMethod <span class="token operator">=</span> oReg<span class="token punctuation">.</span><span class="token function">Methods_</span><span class="token punctuation">(</span><span class="token string">&quot;EnumKey&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> oInParam <span class="token operator">=</span> oMethod<span class="token punctuation">.</span>InParameters<span class="token punctuation">.</span><span class="token function">SpawnInstance_</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    oInParam<span class="token punctuation">.</span>hDefKey <span class="token operator">=</span> <span class="token number">0x80000000</span><span class="token punctuation">;</span>
    oInParam<span class="token punctuation">.</span>sSubKeyName <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> NameAndType <span class="token operator">=</span> oReg<span class="token punctuation">.</span><span class="token function">ExecMethod_</span><span class="token punctuation">(</span>oMethod<span class="token punctuation">.</span>Name<span class="token punctuation">,</span> oInParam<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> arrSubKeys <span class="token operator">=</span> NameAndType<span class="token punctuation">.</span>sNames<span class="token punctuation">.</span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 遍历HKEY_CLASSES_ROOT中所有键</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arrSubKeys<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">var</span> key <span class="token operator">=</span> arrSubKeys<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>key<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">..\.</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">continue</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>key<span class="token punctuation">.</span><span class="token function">search</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">..\..*\.</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">var</span> independent <span class="token operator">=</span> key<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">.+?$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>dict<span class="token punctuation">.</span><span class="token function">Exists</span><span class="token punctuation">(</span>independent<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                dict<span class="token punctuation">.</span><span class="token function">Add</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token keyword">var</span> vdpid <span class="token operator">=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> element <span class="token keyword">in</span> dict<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>element<span class="token punctuation">.</span>length <span class="token operator">&gt;</span> key<span class="token punctuation">.</span>length <span class="token operator">&amp;&amp;</span> element<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span>key<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token operator">==</span> key <span class="token operator">+</span> <span class="token string">&quot;.&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        vdpid <span class="token operator">=</span> element<span class="token punctuation">;</span>
                        <span class="token keyword">break</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>vdpid<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                dict<span class="token punctuation">.</span><span class="token function">Remove</span><span class="token punctuation">(</span>vdpid<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            dict<span class="token punctuation">.</span><span class="token function">Add</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">var</span> retArr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VBArray</span><span class="token punctuation">(</span>dict<span class="token punctuation">.</span><span class="token function">Keys</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        retArr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> retArr<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> fso <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">'Scripting.FileSystemObject'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> ts <span class="token operator">=</span> fso<span class="token punctuation">.</span><span class="token function">CreateTextFile</span><span class="token punctuation">(</span><span class="token string">'本机可用的com组件.txt'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
ts<span class="token punctuation">.</span><span class="token function">Write</span><span class="token punctuation">(</span><span class="token function">listcom</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">'\r\n'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="创建任务计划"><a href="#创建任务计划" class="header-anchor">#</a> 创建任务计划</h3> <p><strong><a href="/Shell/WindowsBatch.html#事件">Windows中的事件</a></strong></p> <p><strong>使用示例见<a href="/files/%E8%AE%BE%E7%BD%AE%E5%BF%85%E5%BA%94%E5%A3%81%E7%BA%B8.bat">设置必应壁纸.bat</a>文件</strong></p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">/**
 * 创建任务计划的常用API展示，在实现中需结合实际使用
 */</span>
<span class="token keyword">function</span> <span class="token function">createSchedule</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 创建TaskService对象，提供对任务计划程序服务的访问权限，以管理已注册的任务</span>
    <span class="token keyword">var</span> service <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ActiveXObject</span><span class="token punctuation">(</span><span class="token string">&quot;Schedule.Service&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/taskservice-connect</span>
    service<span class="token punctuation">.</span><span class="token function">Connect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 获取一个文件夹以在其中创建任务定义。</span>
    <span class="token keyword">var</span> rootFolder <span class="token operator">=</span> service<span class="token punctuation">.</span><span class="token function">GetFolder</span><span class="token punctuation">(</span><span class="token string">&quot;\\&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 返回一个空的任务定义对象，参数保留供将来使用，必须设置为0</span>
    <span class="token keyword">var</span> taskDefinition <span class="token operator">=</span> service<span class="token punctuation">.</span><span class="token function">NewTask</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 创建RegistrationInfo对象，设置任务的注册信息</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/registrationinfo</span>
    <span class="token keyword">var</span> regInfo <span class="token operator">=</span> taskDefinition<span class="token punctuation">.</span>RegistrationInfo<span class="token punctuation">;</span>
    <span class="token comment">// 任务说明</span>
    regInfo<span class="token punctuation">.</span>Description <span class="token operator">=</span> <span class="token string">&quot;任务说明描述&quot;</span><span class="token punctuation">;</span>
    <span class="token comment">// 创建人</span>
    regInfo<span class="token punctuation">.</span>Author <span class="token operator">=</span> <span class="token string">&quot;创建人&quot;</span><span class="token punctuation">;</span>

    <span class="token comment">// 操作集合，运行程序/脚本等动作的集合，最多32个动作</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/actioncollection</span>
    <span class="token keyword">var</span> actions <span class="token operator">=</span> taskDefinition<span class="token punctuation">.</span>Actions<span class="token punctuation">;</span>
    <span class="token comment">// 创建要执行的任务的动作：0运行脚本或程序，5触发处理程序，6发送邮件，7显示一个消息框</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/actioncollection-create</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/action#remarks</span>
    
    <span class="token comment">// 向任务添加操作 https://docs.microsoft.com/zh-cn/windows/win32/taskschd/execaction</span>
    <span class="token keyword">var</span> action <span class="token operator">=</span> actions<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    action<span class="token punctuation">.</span>Path <span class="token operator">=</span> <span class="token string">&quot;wscript&quot;</span><span class="token punctuation">;</span>
    action<span class="token punctuation">.</span>Arguments <span class="token operator">=</span> <span class="token string">&quot;C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\StartUp\\设置必应壁纸.vbs&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> action1 <span class="token operator">=</span> actions<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    action1<span class="token punctuation">.</span>Path <span class="token operator">=</span> <span class="token string">&quot;eventvwr&quot;</span><span class="token punctuation">;</span>

    <span class="token comment">// 提供主体安全证书的脚本对象。这些安全凭证为与委托人关联的任务定义了安全上下文。</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/principal</span>
    <span class="token keyword">var</span> principal <span class="token operator">=</span> taskDefinition<span class="token punctuation">.</span>Principal<span class="token punctuation">;</span>
    <span class="token comment">// 将登录类型设置为交互式登录</span>
    <span class="token comment">// principal.LogonType = 3;</span>
    <span class="token comment">// 获取或设置标识符，该标识符用于指定运行与主体相关联的任务所需的特权级别。</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/principal-runlevel</span>
    principal<span class="token punctuation">.</span>RunLevel <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>

    <span class="token comment">// 创建一个TaskSettings对象，设置任务计划程序的任务设置信息</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/tasksettings</span>
    <span class="token keyword">var</span> settings <span class="token operator">=</span> taskDefinition<span class="token punctuation">.</span>Settings<span class="token punctuation">;</span>
    <span class="token comment">// 该值指示任务计划程序可以在计划时间过去之后的任何时间启动任务</span>
    settings<span class="token punctuation">.</span>StartWhenAvailable <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    settings<span class="token punctuation">.</span>Enabled <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token comment">// 该值指示任务将在UI中不可见</span>
    settings<span class="token punctuation">.</span>Hidden <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取或设置任务的优先级。</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/tasksettings-priority</span>
    settings<span class="token punctuation">.</span>Priority <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token comment">// 获取或设置用于启动任务的触发器的集合。</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/trigger</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/triggercollection-create</span>
    <span class="token keyword">var</span> triggers <span class="token operator">=</span> taskDefinition<span class="token punctuation">.</span>Triggers<span class="token punctuation">;</span>

    
    <span class="token comment">// 创建事件触发器</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/eventtrigger</span>
    <span class="token keyword">var</span> task_trigger_event <span class="token operator">=</span> triggers<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 定义事件查询。触发器将启动任务，当收到事件时。</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/eventtrigger-subscription</span>
    task_trigger_event<span class="token punctuation">.</span>Subscription <span class="token operator">=</span> <span class="token string">&quot;&lt;QueryList&gt;&quot;</span> <span class="token operator">+</span>
        <span class="token string">&quot;&lt;Query Id='0'&gt;&lt;Select Path='System'&gt;&quot;</span> <span class="token operator">+</span>
        <span class="token string">&quot;*[System[Provider[@Name='Microsoft-Windows-Power-Troubleshooter'] and EventID=1]]&quot;</span> <span class="token operator">+</span>
        <span class="token string">&quot;&lt;/Select&gt;&lt;/Query&gt;&quot;</span> <span class="token operator">+</span>
        <span class="token string">&quot;&lt;Query Id='1'&gt;&lt;Select Path='System'&gt;&quot;</span> <span class="token operator">+</span>
        <span class="token string">&quot;*[System/Level=2]&quot;</span> <span class="token operator">+</span>
        <span class="token string">&quot;&lt;/Select&gt;&lt;/Query&gt;&quot;</span> <span class="token operator">+</span>
        <span class="token string">&quot;&lt;/QueryList&gt;&quot;</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取或设置命名XPath查询的集合</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/eventtrigger-valuequeries</span>
    <span class="token keyword">var</span> valueQueries <span class="token operator">=</span> task_trigger_event<span class="token punctuation">.</span>ValueQueries<span class="token punctuation">;</span>
    valueQueries<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token string">&quot;eventID&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Event/System/EventRecordID&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/showmessageaction</span>
    <span class="token keyword">var</span> action7 <span class="token operator">=</span> actions<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    action7<span class="token punctuation">.</span>Title <span class="token operator">=</span> <span class="token string">&quot;标题&quot;</span><span class="token punctuation">;</span>
    <span class="token comment">// 需要配合trigger.ValueQueries</span>
    action7<span class="token punctuation">.</span>MessageBody <span class="token operator">=</span> <span class="token string">&quot;这是事件ID：$(eventID)&quot;</span><span class="token punctuation">;</span>
    
    <span class="token comment">// 创建时间触发器</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/timetrigger</span>
    <span class="token keyword">var</span> task_trigger_time <span class="token operator">=</span> triggers<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">// 创建每日触发器</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/dailytrigger</span>
    <span class="token keyword">var</span> task_trigger_daily <span class="token operator">=</span> triggers<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">// 创建每周触发器</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/weeklytrigger</span>
    <span class="token keyword">var</span> task_trigger_weekly <span class="token operator">=</span> triggers<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    task_trigger_weekly<span class="token punctuation">.</span>DaysOfWeek <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token comment">// 任务每周运行一次。</span>
    task_trigger_weekly<span class="token punctuation">.</span>WeeksInterval <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
    
    <span class="token comment">// 创建根据月度计划启动任务的触发器，在特定月份的特定日期开始</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/monthlytrigger</span>
    <span class="token keyword">var</span> task_trigger_monthly <span class="token operator">=</span> triggers<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">// 创建每月DOWT触发器，按月星期几时间表启动任务的触发器</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/monthlydowtrigger</span>
    <span class="token keyword">var</span> task_trigger_monthlydow <span class="token operator">=</span> triggers<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">// 创建闲置触发，在发生空闲情况时启动任务的触发器</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/idletrigger</span>
    <span class="token keyword">var</span> task_trigger_idle <span class="token operator">=</span> triggers<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">// 创建注册触发器</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/registrationtrigger</span>
    <span class="token keyword">var</span> task_trigger_registration <span class="token operator">=</span> triggers<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">// 创建启动触发器</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/boottrigger</span>
    <span class="token keyword">var</span> task_trigger_boot <span class="token operator">=</span> triggers<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">// 创建登录触发器</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/logontrigger</span>
    <span class="token keyword">var</span> task_trigger_logon <span class="token operator">=</span> triggers<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 登录指定用户时触发，必须是有效的用户帐户</span>
    task_trigger_logon<span class="token punctuation">.</span>UserId <span class="token operator">=</span> <span class="token string">&quot;SYSTEM&quot;</span><span class="token punctuation">;</span>
    
    <span class="token comment">// 用于触发控制台连接或断开连接，远程连接或断开连接或工作站锁定或解锁通知的任务。</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/sessionstatechangetrigger</span>
    <span class="token keyword">var</span> task_session_unlock <span class="token operator">=</span> triggers<span class="token punctuation">.</span><span class="token function">Create</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取或设置连接到会话的状态更改类型：1本地连接，2断开本地连接，3远程连接，4断开远程连接，7锁定，8解锁</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/sessionstatechangetrigger-statechange</span>
    task_session_unlock<span class="token punctuation">.</span>StateChange <span class="token operator">=</span> <span class="token number">8</span><span class="token punctuation">;</span>
    <span class="token comment">// 继承自Trigger对象。获取触发器的类型。</span>
    task_session_unlock<span class="token punctuation">.</span>Type<span class="token punctuation">;</span>


    <span class="token comment">/**
     * 以下为每一个trigger都有的通用属性
     */</span>

    <span class="token comment">// 获取或设置触发器的标识符</span>
    trigger<span class="token punctuation">.</span>Id <span class="token operator">=</span> <span class="token string">&quot;触发器ID&quot;</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取或设置一个布尔值，该值指示是否启用了触发器</span>
    trigger<span class="token punctuation">.</span>Enabled <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取或设置激活触发器的日期和时间。触发器可以在激活触发器后启动任务。</span>
    trigger<span class="token punctuation">.</span>StartBoundary <span class="token operator">=</span> <span class="token string">&quot;2006-05-02T10:49:02&quot;</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取或设置停用触发器的日期和时间。触发器在停用后无法启动任务。</span>
    trigger<span class="token punctuation">.</span>EndBoundary <span class="token operator">=</span> <span class="token string">&quot;2006-05-02T10:52:02&quot;</span><span class="token punctuation">;</span>
    <span class="token comment">// 获取或设置允许触发器启动的任务运行的最长时间，5分钟</span>
    trigger<span class="token punctuation">.</span>ExecutionTimeLimit <span class="token operator">=</span> <span class="token string">&quot;PT5M&quot;</span><span class="token punctuation">;</span>
    <span class="token comment">// 延迟30秒</span>
    trigger<span class="token punctuation">.</span>Delay <span class="token operator">=</span> <span class="token string">&quot;PT30S&quot;</span><span class="token punctuation">;</span>

    <span class="token comment">// 使用ITaskDefinition接口在指定位置注册（创建）任务以定义任务</span>
    <span class="token comment">// 用户ID有：Local Service ; SYSTEM ; null为当前登录的用户名</span>
    <span class="token comment">// 最后一位参数影响任务计划运行</span>
    <span class="token comment">// https://docs.microsoft.com/zh-cn/windows/win32/taskschd/taskfolder-registertaskdefinition</span>
    rootFolder<span class="token punctuation">.</span><span class="token function">RegisterTaskDefinition</span><span class="token punctuation">(</span><span class="token string">&quot;任务计划名&quot;</span><span class="token punctuation">,</span> taskDefinition<span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token string">&quot;用户ID&quot;</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/woytu/notes-vuepress/edit/master//Shell/WindowsJScript.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2020-10-21 11:02:11</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/Shell/WindowsBatch.html" class="prev">
        WindowsBatch
      </a></span> <span class="next"><a href="/Shell/WindowsScript.html">
        WindowsScript
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.269009d6.js" defer></script><script src="/assets/js/3.974b9720.js" defer></script><script src="/assets/js/62.cb825d1a.js" defer></script>
  </body>
</html>
